package com.zixuan.yuojbackendjudgeservice.judge.codesandbox.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.zixuan.yuojbackendcommon.common.ErrorCode;
import com.zixuan.yuojbackendcommon.exception.BusinessException;
import com.zixuan.yuojbackendjudgeservice.judge.codesandbox.CodeSandBox;
import com.zixuan.yuojbackendjudgeservice.judge.codesandbox.model.ExecuteCodeRequest;
import com.zixuan.yuojbackendjudgeservice.judge.codesandbox.model.ExecuteCodeResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Mr.Mei
 * @date 2023/11/14
 */
@Slf4j
public class RemoteCodeSandBox implements CodeSandBox {

    @Override
    public ExecuteCodeResponse executeSandBox(ExecuteCodeRequest request) {
        String url = "http://192.168.229.128:90/doJudge";
//        request.setInputList(Arrays.asList("1 2", "3 5", "9 12"));
//        String code = "public class Main {\n" +
//                "    public static void main(String[] args) {\n" +
//                "        Integer a = Integer.parseInt(args[0]);\n" +
//                "        Integer b = Integer.parseInt(args[1]);\n" +
//                "        System.out.println(a+b);\n" +
//                "    }\n" +
//                "}\n";
        String code = request.getCode();
        request.setCode(code);
        request.setLanguage("java");
        String jsonStr = JSONUtil.toJsonStr(request);
        String responseStr = HttpUtil.createPost(url)
                .header("auth","secretKey")
                .body(jsonStr)
                .execute()
                .body();
        if(StrUtil.isBlank(responseStr)){
            throw new BusinessException(ErrorCode.SYSTEM_API_ERROR,"响应为空");
        }
        ExecuteCodeResponse executeCodeResponse = JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
        return executeCodeResponse;
    }
}
